package top.yongyan0624.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import top.yongyan0624.common.lang.Result;
import top.yongyan0624.entity.Shop;
import top.yongyan0624.entity.Vote;
import top.yongyan0624.entity.VoteUser;

import java.io.IOException;
import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yongyan
 * @since 2021-09-16
 */
@RestController
@RequestMapping("/shop")
public class ShopController extends BaseController {
    @GetMapping("/list")
    public Result list(String type, String name) {
        Page<Shop> pageData = shopService.page(getPage(), new QueryWrapper<Shop>().eq(StrUtil.isNotBlank(type),"type",type)
                .like(StrUtil.isNotBlank(name), "name", name).orderByDesc("create_time"));

        pageData.getRecords().forEach(s -> {
            s.setUser(sysUserService.getById(s.getUserId()));
            s.setImgList(StrUtil.split(s.getImg(), ',' ,true,true));
        });

        return Result.success(pageData);
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        Shop shop = shopService.getById(id);

        shop.setUser(sysUserService.getById(shop.getUserId()));
        shop.setImgList(StrUtil.split(shop.getImg(), ',' ,true,true));

        return Result.success(shop);
    }

    @PostMapping("/save")
    public Result save(@RequestPart Shop shop, @RequestPart MultipartFile[] imgFiles) {
        try {
            if (imgFiles.length <= 0) {
                return Result.fail("文件为空");
            }
            //文件不为空，处理文件，存储文件并生成随机虚拟路径文件名
            String fileName = handleFile("shop", shop.getUserId(), imgFiles);

            //设置img虚拟路径
            shop.setImg(fileName);

        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            return Result.fail("图片上传失败！");
        }

        shopService.save(shop);

        shop.setUser(sysUserService.getById(shop.getUserId()));
        shop.setImgList(StrUtil.split(shop.getImg(), ',' ,true,true));

        return Result.success(shop);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('shop:update')")
    public Result update(@RequestPart Shop shop, @RequestPart MultipartFile[] imgFiles){
        try {
            if (imgFiles.length > 0) {
                //文件不为空，处理文件，存储文件并生成随机虚拟路径文件名
                String fileName = handleFile("shop", shop.getUserId(), imgFiles);

                //设置img虚拟路径
                shop.setImg(fileName);
            }
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            return Result.fail("图片上传失败！");
        }

        shopService.updateById(shop);

        shop.setUser(sysUserService.getById(shop.getUserId()));
        shop.setImgList(StrUtil.split(shop.getImg(), ',' ,true,true));

        return Result.success(shop);
    }

    @Transactional
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('shop:delete')")
    public Result delete(@RequestBody Long[] ids) {
        shopService.removeByIds(Arrays.asList(ids));

        return Result.success("");
    }
}
